package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auth: cdz
 * @Desc:
 * @Date: 2023/3/8 8:29
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 功能描述: 添加套餐
     *
     * @return : void
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //保存套餐
        setmealMapper.save(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        List<SetmealDish> collect = setmealDishes.stream().map(setmealDish -> {
            Long id = setmeal.getId();
            //设置套餐菜品关系里面的菜品id
            setmealDish.setSetmealId(id);
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishMapper.saveBatch(collect);
    }

    /**
     * 功能描述: 套餐分页
     *
     * @return : void
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        List<SetmealVO> collect = page.getResult().stream().map(setmeal -> {
            //返回页面需要的vo
            SetmealVO setmealVO = new SetmealVO();
            //将查询出的套餐数据拷贝到vo
            BeanUtils.copyProperties(setmeal, setmealVO);
            //设置套餐里面的分类名字也需要查询
            String nameByCategoryId = categoryMapper.findNameByCategoryId(setmeal.getCategoryId());
            setmealVO.setCategoryName(nameByCategoryId);
            //前端需要展示什么返回什么
            return setmealVO;
        }).collect(Collectors.toList());
        return new PageResult(page.getTotal(), collect);
    }

    /**
     * 功能描述: 套餐停售起售
     *
     * @return : void
     */
    @Override
    public void editStatus(Integer status, Long id) {
        //判断套餐里面是否有菜品停售
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                dishList.forEach(dish -> {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }

    /**
     * 功能描述: 批量删除
     *
     * @return : void
     */
    @Override
    public void delete(List<Long> ids) {
        List<Long> collect = ids.stream().map(id -> {
            //判断是否起售
            Setmeal setmeal = setmealMapper.findById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //起售中,不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            return id;
        }).collect(Collectors.toList());
        //批量删除
        setmealMapper.deleteSetmealBatch(collect);
        setmealDishMapper.deleteSetmealDishBatch(collect);
    }

    /**
     * 功能描述: 套餐修改
     *
     * @return : void
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        //获取套餐id
        Long id = setmeal.getId();
        //删除套餐和菜品关联表
        setmealDishMapper.deleteBySetmealId(id);
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });
        //重新插入套餐菜品关系
        setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 功能描述: 根据id查询
     * 回显套餐里面的数据
     *
     * @return : void
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {

        Setmeal setmeal = setmealMapper.findById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.findDishListBySetMealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 条件查询
     *
     * @param setmealDTO
     * @return
     */
    public List<Setmeal> list(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
}
